package com.bjc.tt_shop.web;

import com.bjc.tt_shop.dto.Order;
import com.bjc.tt_shop.dto.Page;
import com.bjc.tt_shop.dto.Result;
import com.bjc.tt_shop.pojo.po.TbItem;
import com.bjc.tt_shop.service.ItemService;


import com.bjc.tt_shop.vo.TbItemCustom;
import com.bjc.tt_shop.vo.TbItemQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * User: DHC
 * Date: 2017/12/29
 * Time: 14:13
 * Version:V1.0
 */
@Controller
public class ItemAction {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ItemService itemService;


    @ResponseBody
    @RequestMapping(value = "/item/{itemId}",method = RequestMethod.GET)
    public TbItem getItemById(@PathVariable("itemId") Long itemId){
        return itemService.getItemById(itemId);
    }

  /*@ResponseBody
    @RequestMapping(value = "/item",method=RequestMethod.POST)
    public List<TbItem> listItem(){
        List<TbItem> list = null;
        try{
            list = itemService.listItem();
            System.out.println(list);

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }
        return list;
    }*/
  //保存商品
  @ResponseBody
  @RequestMapping(value = "/item", method =RequestMethod.POST)
  public int saveItem(TbItem tbItem ,String itemDesc){
      int i = 0;
      try{
          i = itemService.saveItem(tbItem,itemDesc);
      }catch (Exception e){
          logger.error(e.getMessage(),e);
          e.printStackTrace();
      }
      return i;

  }

//分页
 @ResponseBody
 @RequestMapping(value = "/items",method=RequestMethod.GET)
 public Result<TbItemCustom> listItem(Page page, Order order, TbItemQuery query){
     System.out.println(page);
     Result<TbItemCustom> result = null;
     try{
         result =itemService.listItemsByPage(page,order,query);
         System.out.println(result);

     }catch (Exception e){
         logger.error(e.getMessage(),e);
         e.printStackTrace();
     }
     return result;
 }
//逻辑删除
 @ResponseBody
 @RequestMapping(value = "item/batch",method = RequestMethod.POST)
    public int removeItemByStatus(@RequestParam("ids[]") List<Long> ids){
     int i = 0;
         try{
            i = itemService.batchUpdate(ids);
         }catch (Exception e){
             logger.error(e.getMessage(),e);
             e.printStackTrace();
         }
         return i;
     }
  //上架商品
  @ResponseBody
  @RequestMapping(value = "item/batchUp",method = RequestMethod.POST)
  public int  batchItemUp(@RequestParam("ids[]") List<Long> ids){
      int i = 0;
      try{
          i = itemService.batchItemUp(ids);
      }catch (Exception e){
          logger.error(e.getMessage(),e);
          e.printStackTrace();
      }
      return i;
  }

  //下架商品
    @ResponseBody
    @RequestMapping(value = "item/batchDown",method = RequestMethod.POST)
    public int  batchItemDown(@RequestParam("ids[]") List<Long> ids){
        int i = 0;
        try{
            i = itemService.batchItemDown(ids);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }
        return i;
    }
}
